﻿///////////////////////////////////////////////////////////////////////////////
// 文件名： Camera.cc
// 作者：   671643387@qq.com
// 日期：   2015年11月1日 下午6:42:47
// 简介：   
///////////////////////////////////////////////////////////////////////////////

#include <memory.h>
#include <cassert>
#include "graphics/graphics/render/Render.hpp"

#include "graphics/graphics/scenenode/Camera.hpp"
using namespace ::graphics::graphics::scenenode;

Camera::Camera()
    : id_(0)
    , index_(0)
    , clearColor_(0xff012456)
    , scene_(NULL)
{
    memset(&properties_, 0, sizeof(properties_));
    properties_.projection = Camera::Perspective;
    properties_.aspect = 1.0f;
    properties_.fovy = ::graphics::PI / 4;
    properties_.near_ = 1.0f;
    properties_.far_ = 1000.0f;
    properties_.width = 800.0f;
    properties_.height = 600.0f;
    properties_.lookat.x = 0.0f; properties_.lookat.y = 0.0f; properties_.lookat.z = 0.0f;
    properties_.position.x = 0.0f; properties_.position.y = 0.0f; properties_.position.z = -10.0f;
    properties_.upvec.x = 0.0f; properties_.upvec.y = 1.0f; properties_.upvec.z = 0.0f;
}

Camera::~Camera()
{

}

///////////////////////////////////////////////////////////////////////////////

int Camera::GetID() const
{
    return id_;
}

int Camera::GetIndex() const
{
    return index_;
}

bool Camera::BeginFrame()
{
    assert(scene_ != NULL);
    scene_->GetRender()->ClearColor(
        (clearColor_ & 0xff0000) >> 16,
        (clearColor_ & 0xff00) >> 8,
        (clearColor_ & 0xff),
        (clearColor_ & 0xff000000) >> 24
        );
    if (scene_->GetRender()->BeginFrame())
    {
        scene_->GetRender()->UseCamera(properties_);
        return true;
    }

    return false;
}

void Camera::EndFrame()
{
    assert(scene_ != NULL);
    scene_->GetRender()->EndFrame();
}

void Camera::SetScene(::graphics::graphics::Scene* scene)
{
    scene_ = scene;
}

void Camera::SetClearColor(::graphics::byte r, ::graphics::byte g, ::graphics::byte b, ::graphics::byte a)
{
    clearColor_ = (r << 16) | (g << 8) | b | (a << 24);
}

///////////////////////////////////////////////////////////////////////////////